//Given an array nums of distinct integers, return all the possible 
//permutations. You can return the answer in any order. 
//
// 
// Example 1: 
// Input: nums = [1,2,3]
//Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
// Example 2: 
// Input: nums = [0,1]
//Output: [[0,1],[1,0]]
// Example 3: 
// Input: nums = [1]
//Output: [[1]]
// 
// 
// Constraints: 
//
// 
// 1 <= nums.length <= 6 
// -10 <= nums[i] <= 10 
// All the integers of nums are unique. 
// 
// Related Topics Array Backtracking 👍 8218 👎 160


package leetcode.editor.en;

import java.util.ArrayList;
import java.util.List;

public class _46_Permutations {
    public static void main(String[] args) {
        Solution solution = new _46_Permutations().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<List<Integer>> permute(int[] nums) {
            List<List<Integer>> result = new ArrayList<>();
            dfs(nums, new ArrayList<>(), 0, result);
            return result;
        }

        private void dfs(int[] nums, ArrayList<Integer> curPath, int cur, List<List<Integer>> result) {
            if (nums.length == curPath.size()) {
                result.add(new ArrayList<>(curPath));
                return;
            }
            for (int i = cur; i < nums.length; i++) {
                if(curPath.contains(nums[i])){
                    continue;
                }
                curPath.add(nums[i]);
                dfs(nums, curPath, cur, result);
                curPath.remove(curPath.size()-1);
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}